# -*- encoding: utf-8 -*-
"""
@File    : field_mapping.py
@Time    : 2021/9/1 15:44
@Author  : zhouys4
"""
# 订单编号
order_number_list = ['订单号', '联想订单号', '订单编号']
# 下单时间
order_date_list = ['系统日期', '销售订单日期', '创建时间', '销售日期']
# 物料编号
material_order_list = ['物料编码', '物料', '物料号', '商品编号', ]
# 物料描述
material_describe_list = ['物料说明', '物料名称', '物料', '商品名称']
# 总金额
total_prices_list = ['代理价（本位币）', '税前金额（本位币）', '支付金额', '销售金额']
# 单价
unit_price_list = []
# 数量
number_list = ['销量', '销量（无单位）', '商品数量']
# 代理商编号
agent_list = ['代理', '分销商编号','客户编号']
# 代理商名称
agent_name_list = ['代理名称', '代理', '代理商名称', '分销商名称', '代理商','客户名称']
# 产品型号
product_model_list = ['机型']

# 字段映射
def columns_update(df):
    # 订单号
    if '订单编号' in df.columns:
        df.rename(columns={'订单编号': 'order_number'}, inplace=True)
    elif '订单号' in df.columns:
        df.rename(columns={'订单号': 'order_number'}, inplace=True)
    elif '联想订单号' in df.columns:
        df.rename(columns={'联想订单号': 'order_number'}, inplace=True)
    # 总金额
    if '订单产品总价' in df.columns:
        df.rename(columns={'订单产品总价': 'total_prices'}, inplace=True)
    elif '总金额' in df.columns:
        df.rename(columns={'总金额': 'total_prices'}, inplace=True)
    elif '订单总价' in df.columns:
        df.rename(columns={'订单总价': 'total_prices'}, inplace=True)
    elif 'sum(订单产品总价_含税RMB)' in df.columns:
        df.rename(columns={'sum(订单产品总价_含税RMB)': 'total_prices'}, inplace=True)
    elif '订单产品总价_含税RMB' in df.columns:
        df.rename(columns={'订单产品总价_含税RMB': 'total_prices'}, inplace=True)
    elif '订单产品总价(BW)￥' in df.columns:
        df.rename(columns={'订单产品总价(BW)￥': 'total_prices'}, inplace=True)
    elif '税前金额（本位币）' in df.columns:
        df.rename(columns={'税前金额（本位币）': 'total_prices'}, inplace=True)
    # 数量
    if '销量汇总' in df.columns:
        df.rename(columns={'销量汇总': 'number'}, inplace=True)
    elif '产品数量' in df.columns:
        df.rename(columns={'产品数量': 'number'}, inplace=True)
    elif '销量（无单位）' in df.columns:
        df.rename(columns={'销量（无单位）': 'number'}, inplace=True)
    elif '订单数量' in df.columns:
        df.rename(columns={'订单数量': 'number'}, inplace=True)
    elif 'sum(产品数量的)' in df.columns:
        df.rename(columns={'sum(产品数量的)': 'number'}, inplace=True)
    elif '产品数量的' in df.columns:
        df.rename(columns={'产品数量的': 'number'}, inplace=True)
    elif '主机销量' in df.columns:
        df.rename(columns={'主机销量': 'number'}, inplace=True)
    elif 'sum(主机销量)' in df.columns:
        df.rename(columns={'sum(主机销量)': 'number'}, inplace=True)

    # 物料编号
    if '物料编号' in df.columns:
        df.rename(columns={'物料编号': 'material'}, inplace=True)
    elif '物料号' in df.columns:
        df.rename(columns={'物料号': 'material'}, inplace=True)
    elif '商品编号' in df.columns:
        df.rename(columns={'商品编号': 'material'}, inplace=True)
    elif '物料' in df.columns:
        df.rename(columns={'物料': 'material'}, inplace=True)

    # 物料描述
    # '物料说明', '物料名称', '物料', '商品名称'
    if '物料名称' in df.columns:
        df.rename(columns={'物料名称': 'material_describe'}, inplace=True)
    elif '商品名称' in df.columns:
        df.rename(columns={'商品名称': 'material_describe'}, inplace=True)
    elif '物料' in df.columns:
        df.rename(columns={'物料': 'material_describe'}, inplace=True)
    elif '物料说明' in df.columns:
        df.rename(columns={'物料说明': 'material_describe'}, inplace=True)
    elif '产品名称' in df.columns:
        df.rename(columns={'产品名称': 'material_describe'}, inplace=True)

    # 订单日期
    # '系统日期', '销售订单日期', '创建时间', '销售日期'
    if '下单时间' in df.columns:
        df.rename(columns={'下单时间': 'order_date'}, inplace=True)
    elif '订单时间' in df.columns:
        df.rename(columns={'订单时间': 'order_date'}, inplace=True)
    elif '销售订单日期' in df.columns:
        df.rename(columns={'销售订单日期': 'order_date'}, inplace=True)
    elif '系统日期' in df.columns:
        df.rename(columns={'系统日期': 'order_date'}, inplace=True)
    elif '创建时间' in df.columns:
        df.rename(columns={'创建时间': 'order_date'}, inplace=True)
    elif '销售日期' in df.columns:
        df.rename(columns={'销售日期': 'order_date'}, inplace=True)
    elif '订单日期' in df.columns:
        df.rename(columns={'订单日期': 'order_date'}, inplace=True)
    # 代理商编号
    if '代理编号' in df.columns:
        df.rename(columns={'代理编号': 'agent'}, inplace=True)
    elif '代理' in df.columns:
        df.rename(columns={'代理': 'agent'}, inplace=True)
    elif '分销商编号' in df.columns:
        df.rename(columns={'分销商编号': 'agent'}, inplace=True)
    elif '代理名称' in df.columns:
        df.rename(columns={'代理名称': 'agent'}, inplace=True)
    elif 'T1名称' in df.columns:
        df.rename(columns={'T1名称': 'agent'}, inplace=True)
    elif '代理商名称' in df.columns:
        df.rename(columns={'代理商名称': 'agent'}, inplace=True)
    elif '代理商全称' in df.columns:
        df.rename(columns={'代理商全称': 'agent'}, inplace=True)

    # 代理商名称
    if '代理名称' in df.columns:
        df.rename(columns={'代理名称': 'agent_name'}, inplace=True)
    elif '代理' in df.columns:
        df.rename(columns={'代理': 'agent_name'}, inplace=True)
    elif '分销商名称' in df.columns:
        df.rename(columns={'分销商名称': 'agent_name'}, inplace=True)
    elif '代理商名称' in df.columns:
        df.rename(columns={'代理商名称': 'agent_name'}, inplace=True)
    elif '代理商' in df.columns:
        df.rename(columns={'代理商': 'agent_name'}, inplace=True)

    return df
    # 如果有税前金额（本位币）和税前金额的表用税前金额（本位币）代替订单产品总价

#判断字段是否存在
def file_check(df):
    # 判断订单号是否存在
    """
    订单号
    len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0
    总金额
    len(list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0
    数量
    len(list(set(number_list).intersection(set(df.columns.to_list())))) == 0
    物料编号
    len(list(set(material_order_list).intersection(set(df.columns.to_list())))) == 0
    物料描述
    len(list(set(material_describe_list).intersection(set(df.columns.to_list())))) == 0
    订单日期
    len(list(set(order_date_list).intersection(set(df.columns.to_list())))) == 0
    代理商编号
    len(list(set(agent_list).intersection(set(df.columns.to_list())))) == 0
    代理商名称
    len(list(set(agent_name_list).intersection(set(df.columns.to_list())))) == 0
    单价
    len(list(set(unit_price_list).intersection(set(df.columns.to_list())))) == 0
    产品型号
    len(list(set(product_model_list).intersection(set(df.columns.to_list())))) == 0
    :param df:
    :return:
    """
    if len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0:
        return '0'
    elif len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0 and len(
            list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0:
        return '10'
    elif len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0 and len(
            list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(number_list).intersection(set(df.columns.to_list())))) == 0:
        return '110'
    elif len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0 and len(
            list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(number_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_order_list).intersection(set(df.columns.to_list())))) == 0:
        return '1110'
    elif len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0 and len(
            list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(number_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_order_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_describe_list).intersection(set(df.columns.to_list())))) == 0:
        return '11110'
    elif len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0 and len(
            list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(number_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_order_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_describe_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(order_date_list).intersection(set(df.columns.to_list())))) == 0:
        return '111110'
    elif len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0 and len(
            list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(number_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_order_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_describe_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(order_date_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(agent_list).intersection(set(df.columns.to_list())))) == 0:
        return '1111110'
    elif len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0 and len(
            list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(number_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_order_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_describe_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(order_date_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(agent_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(agent_name_list).intersection(set(df.columns.to_list())))) == 0:
        return '11111110'
    elif len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0 and len(
            list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(number_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_order_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_describe_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(order_date_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(agent_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(agent_name_list).intersection(set(df.columns.to_list())))) == 0:
        return '111111110'
    elif len(list(set(order_number_list).intersection(set(df.columns.to_list())))) == 0 and len(
            list(set(total_prices_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(number_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_order_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(material_describe_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(order_date_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(agent_list).intersection(set(df.columns.to_list())))) == 0 \
            and len(list(set(agent_name_list).intersection(set(df.columns.to_list())))) == 0:
        return '1111111110'
    else:
        return '1111111111'
